package com.kugua.zfms.shardjdbc;


import io.shardingjdbc.core.api.ShardingDataSourceFactory;
import io.shardingjdbc.core.api.config.ShardingRuleConfiguration;
import io.shardingjdbc.core.api.config.TableRuleConfiguration;
import io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration;
import org.apache.commons.dbcp.BasicDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>TODO</p>
 * <p>
 * <PRE>
 * <BR>    修改记录
 * <BR>-----------------------------------------------
 * <BR>    修改日期         修改人          修改内容
 * </PRE>
 *
 * @author cuiyh9
 * @version 1.0
 * @Date Created in 2018年12月20日 18:05
 * @since 1.0
 */
public class ShardingDemo {

    public static void main(String[] args) {
        ShardingDemo shardingDemo = new ShardingDemo();
        shardingDemo.insert();
    }

    public void insert () {
        try {
            DataSource  dataSource = getDataSource();
            Connection connection = dataSource.getConnection();
            for (int i = 1; i < 20; i++) {
                PreparedStatement ps = connection.prepareStatement("insert into t_order (`name`, user_id) VALUES ('张三',"+i+")");
                int size = ps.executeUpdate();
                System.out.println("size:" + size);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public DataSource getDataSource() {
        // 配置真实数据源
        Map<String, DataSource> dataSourceMap = new HashMap<>();

        // 配置第一个数据源
        BasicDataSource dataSource1 = new BasicDataSource();
        dataSource1.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource1.setUrl("jdbc:mysql://localhost:3306/db_0?useUnicode=true&serverTimezone=UTC&characterEncoding=utf-8&autoReconnect=true&useSSL=false");
        dataSource1.setUsername("root");
        dataSource1.setPassword("ziroom");
        dataSourceMap.put("db_0", dataSource1);

        // 配置第二个数据源
        BasicDataSource dataSource2 = new BasicDataSource();
        dataSource2.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource2.setUrl("jdbc:mysql://localhost:3306/db_1?useUnicode=true&serverTimezone=UTC&characterEncoding=utf-8&autoReconnect=true&useSSL=false");
        dataSource2.setUsername("root");
        dataSource2.setPassword("ziroom");
        dataSourceMap.put("db_1", dataSource2);

        // 配置Order表规则
        TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration();
        orderTableRuleConfig.setLogicTable("t_order");
        orderTableRuleConfig.setActualDataNodes("db_${0..1}.t_order${0..1}");

        // 配置分库 + 分表策略
        orderTableRuleConfig.setDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "db_${user_id % 2}"));
        orderTableRuleConfig.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "t_order${user_id % 2}"));

        // 配置分片规则
        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);


        // 获取数据源对象
        DataSource dataSource = null;
        try {
            dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, new ConcurrentHashMap(), new Properties());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dataSource;
    }
}
